package com.brillilab.service.core.method.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.method.MethodTypeMapper;
import com.brillilab.dao.mapper.method.MethodTypeUseMapper;
import com.brillilab.domain.enums.method.MethodTypeUseTypeEnum;
import com.brillilab.domain.po.method.MethodType;
import com.brillilab.domain.po.method.MethodTypeUse;
import com.brillilab.service.core.method.IMethodTypeUseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 *  方法类型使用记录
 *  @author WuMenghao
 *  @since 2019-02-28
 */
@Service
@Transactional
public class MethodTypeUseServiceImpl implements IMethodTypeUseService {

    @Resource
    private MethodTypeUseMapper methodTypeUseMapper;
    @Resource
    private MethodTypeMapper methodTypeMapper;


    /**
     * 方法使用记录插入更新
     * @param thirdId
     * @param userId
     * @return
     */
    @Override
    public boolean insertOrUpdateThirdMethodTypeUse(Long thirdId,Long userId,MethodTypeUseTypeEnum type) {
        Assert.isTrue(thirdId!=null && userId!=null,"参数缺失！");
        MethodTypeUse use=methodTypeUseMapper.selectOne(
                new LambdaQueryWrapper<MethodTypeUse>()
                        .eq(MethodTypeUse::getMethodTypeId,thirdId)
                        .eq(MethodTypeUse::getType,type.getValue())
                        .eq(MethodTypeUse::getUserId,userId));
        if(use == null){
            MethodType methodType=methodTypeMapper.selectById(thirdId);
            if(methodType!=null){
                MethodTypeUse newUse=new MethodTypeUse();
                newUse.setMethodTypeId(thirdId);
                newUse.setUserId(userId);
                newUse.setType(type.getValue());
                newUse.setFirstId(methodType.getFirstId());
                newUse.setSecondId(methodType.getSecondId());
                return methodTypeUseMapper.insert(newUse) > 0;
            }
            return false;
        }else {
            use.setUpdateTime(null);
            use.setUseTime(new Date());
            return methodTypeUseMapper.setUpdateTimeAndAddUseTimes(use) >0;
        }
    }

    /**
     * 方法使用记录插入更新批量
     * @param thirdIds
     * @param userId
     * @return
     */
    @Override
    public boolean insertOrUpdateThirdMethodTypeUseBatch(List<Long> thirdIds,Long userId,MethodTypeUseTypeEnum type) {
        Assert.isTrue(thirdIds!=null && userId!=null,"参数缺失！");
        Date now=new Date();
        if(thirdIds.size()>0){
            List<MethodTypeUse> methodTypeUses=methodTypeUseMapper.selectList(
                    new LambdaQueryWrapper<MethodTypeUse>()
                            .eq(MethodTypeUse::getUserId,userId)
                            .eq(MethodTypeUse::getType,type.getValue()));
            List<Long> exists=methodTypeUses.stream().map(MethodTypeUse::getMethodTypeId).distinct().collect(Collectors.toList());
            List<Long> oldIds=thirdIds.stream().filter(exists::contains).distinct().collect(Collectors.toList());
            List<Long> newIds=thirdIds.stream().filter(e -> !exists.contains(e)).distinct().collect(Collectors.toList());

            List<MethodType> methodTypes=methodTypeMapper.selectBatchIds(thirdIds);

            List<MethodTypeUse> updates=methodTypeUses.stream().filter(e -> oldIds.contains(e.getMethodTypeId())).collect(Collectors.toList());
            for (MethodTypeUse update:updates){
                update.setUseTime(now);
            }

            List<MethodTypeUse> inserts=new ArrayList<>();
            for (Long typeId :newIds){
                MethodType methodType=methodTypes.stream().filter(e -> e.getId().equals(typeId)).findFirst().orElse(null);
                if(methodType!=null){
                    MethodTypeUse newUse=new MethodTypeUse();
                    newUse.setMethodTypeId(typeId);
                    newUse.setUserId(userId);
                    newUse.setUseTime(now);
                    newUse.setType(type.getValue());
                    newUse.setFirstId(methodType.getFirstId());
                    newUse.setSecondId(methodType.getSecondId());
                    inserts.add(newUse);
                }
            }

            if(inserts.size()>0){
                methodTypeUseMapper.insertBatch(inserts,userId);
            }

            if(updates.size()>0){
                methodTypeUseMapper.updateBatch(updates,userId);
            }
        }
        return false;
    }
}
